package com.hyfrogx.modules.information.controller;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hyfrogx.common.utils.QueryExt;
import com.hyfrogx.common.utils.R;
import com.hyfrogx.modules.app.utils.StringUtils;
import com.hyfrogx.modules.information.entity.InfoCommentEntity;
import com.hyfrogx.modules.information.form.CommentStateForm;
import com.hyfrogx.modules.information.service.InfoCommentService;
import com.hyfrogx.modules.sys.controller.AbstractController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author liubin
 * @date 2019/11/20 13:43
 */

@RestController
@RequestMapping(value = "information/comment")
@Api(value = "HyInformationCommentController", tags = {"后台--资讯评论管理"})
public class HyInformationCommentController extends AbstractController {
  @Autowired
  private InfoCommentService infoCommentService;

  /*
   * 获取当前资讯全部评论
   *
   * @return
   */
  @RequestMapping(value = "getInformationComment", method = RequestMethod.GET)
  @ApiOperation("获取当前资讯全部评论")
  public R getInformationComment(@RequestParam Map<String, Object> map) {
    if(!map.containsKey("infoId")){
      return R.error("请选择资讯");
    }
    try {
      Page page = infoCommentService.getInformationComment(new QueryExt<>(map));
      return R.ok().put("result",page);
    } catch (Exception e) {
      return R.error(e.getMessage());
    }
  }

  /*
   * pc端获取当前资讯全部评论
   *
   * @return
   */
  @RequestMapping(value = "getInformationComments", method = RequestMethod.GET)
  @ApiOperation("pc端获取当前资讯全部评论")
  public R getInformationComments(@RequestParam Map<String, Object> map) {
    try {
      Page page;
      if (ObjectUtil.isNotEmpty(map.get("publisher"))) {
        page = infoCommentService.getOldInformationComments(new QueryExt<>(map));
      } else {
        page = infoCommentService.getAllInformationComments(new QueryExt<>(map));
      }
      return R.ok().put("result",page);
    } catch (Exception e) {
      return R.error(e.getMessage());
    }
  }

  /*
   * 获取回复列表
   *
   * @return
   */
  @RequestMapping(value = "comments/replyList", method = RequestMethod.GET)
  @ApiOperation("获取回复列表")
  public R getReplyComments(@RequestParam Map<String, Object> map) {
    try {
      Page result;
      if (ObjectUtil.isEmpty(map.get("reply_uid"))){
        return R.error("请输入回复用户uid");
      }
      if (ObjectUtil.isEmpty(map.get("id"))){
        return R.error("请输入id");
      }
      if (ObjectUtil.isNotEmpty(map.get("uid"))) {
        if (ObjectUtil.isNotEmpty(map.get("publisher"))) {
          result = infoCommentService.getAllReplyComments(new QueryExt<>(map));
        } else {
          result = infoCommentService.getReplyCommentsByUid(new QueryExt<>(map));
        }
      } else {
        return R.error("请选择用户");
      }
      return R.ok().put("result",result);
    } catch (Exception e) {
      return R.error(e.getMessage());
    }
  }

  /*
   * 删除当前资讯评论
   *
   * @return
   */
  // @RequiresPermissions(value = { "info:deleteInformationComment",
  // "info:content" }, logical = Logical.OR)
  @RequestMapping(value = "deleteInformationComment", method = RequestMethod.DELETE)
  @ApiOperation("删除当前资讯评论")
  public R deleteInformationComment(Integer id) {
    try {
      infoCommentService.removeById(id);
      return R.ok("操作成功");
    } catch (Exception e) {
      return R.error(e.getMessage());
    }
  }

  /**
   * 回复评论
   *
   * @return
   */
  @RequestMapping(value = "comment/reply", method = RequestMethod.POST)
  @ApiOperation("回复评论")
  public R replyCommect(@RequestBody InfoCommentEntity infoComment) {
    try {
      if(infoComment.getReplyuid()==null){
        return R.error("请选择回复的评论");
      }
      if (infoComment.getUid() != null) {
        int result = infoCommentService.replyCommet(infoComment, infoComment.getReplyuid());
        if (result == 1) {
          return R.ok("回复成功");
        } else {
          return R.error("只有管理员可以回复");
        }
      } else {
        return R.error("请先登录");
      }
    } catch (Exception e) {
      return R.error(e.getMessage());
    }
  }

  /**
   * 批量删除评论
   *
   * @return
   */
  @RequestMapping(value = "comment/batchDel", method = RequestMethod.POST)
  @ApiOperation("批量删除评论")
  public R delCommect(@RequestBody CommentStateForm form) {
    try {
      if(StringUtils.isBlank(form.getJsonStr())){
        return R.error("请选择评论id");
      }
      JSONArray jsonArray = JSONArray.parseArray(form.getJsonStr());
      for (int i = 0; i < jsonArray.size(); i++) {
        InfoCommentEntity comment = jsonArray.getObject(i, InfoCommentEntity.class);
        // 评论下有回复
        infoCommentService.removeById(comment.getId());
      }
      return R.ok("批量删除评论成功");
    } catch (Exception e) {
      return R.error(e.getMessage());
    }
  }

  /**
   * 批量审核
   *
   * @return
   */
  @RequestMapping(value = "comment/batchUpdateState", method = RequestMethod.POST)
  @ApiOperation("批量审核")
  public R batchUpdateState(@RequestBody CommentStateForm form) {
    try {
      if(StringUtils.isBlank(form.getJsonStr())){
        return R.error("请选择评论id");
      }
      if(form.getState()==null){
        return R.error("请选择审核状态");
      }
      JSONArray jsonArray = JSONArray.parseArray(form.getJsonStr());
      for (int i = 0; i < jsonArray.size(); i++) {
        InfoCommentEntity comment = jsonArray.getObject(i, InfoCommentEntity.class);
        // 评论下有回复
        comment.setState(form.getState());
        infoCommentService.updateById(comment);
      }
      return R.ok("批量审核成功");
    } catch (Exception e) {
      return R.error(e.getMessage());
    }
  }

  @RequestMapping(value = "comment/updateState", method = RequestMethod.POST)
  @ApiOperation("审核评论")
  public R updateState(@RequestBody CommentStateForm form) {
    try {
      if(form.getId()==null){
        return R.error("请选择评论");
      }
      if(form.getState()==null){
        return R.error("请选择审核状态");
      }
      InfoCommentEntity comment = infoCommentService.getBaseMapper().selectById(form.getId());
      if(ObjectUtil.isNull(comment)){
        return R.error("此评论不存在");
      }
      // 评论下有回复
      comment.setState(form.getState());
      infoCommentService.updateById(comment);
      return R.ok("审核成功");
    } catch (Exception e) {
      return R.error(e.getMessage());
    }
  }
}
